<?php

namespace App\Observers;

use App\Models\Article\ArticleOrder;
use App\Models\Setting\SysSetting;
use App\Models\User\MemberOrder;
use App\Models\User\User;
use EasyWeChat\Factory;

class MemberOrderObserver
{

    public function saved(MemberOrder $memberOrder) {
        $user = User::where('id',$memberOrder->user_id)->first();
        $isAuto =  $memberOrder->is_auto;
        $title = $isAuto==1?'素材':'文库';
        if($memberOrder->is_finished==1 && !empty($user) ){
            $config = config('wechat.default');
            $app = Factory::officialAccount($config);
            $templateId = 'ipXgJ89lndIRhq5enMyrzYenZSJ4ZeNAAg4Sp65skEI';
            $openids = [
                'oU4Mm6DyveBqszbNBkbcHVFuAtmw',
                //'oU4Mm6Onl4pbgnvPjkwLHBDdajvY',
            ];
            $keyMap = ['first','month','quarter','year','exfirst','exmonth','exquarter','exyear'];
            $mapMemberType = SysSetting::whereIn('key',$keyMap)->pluck('item_desc','key')->toArray();

            $todayFee = MemberOrder::whereDate('created_at',date('Y-m-d'))->where('is_finished',1)->where('is_auto',$isAuto)->sum('fee')+
                ArticleOrder::whereDate('created_at',date('Y-m-d'))->where('is_finished',1)->where('is_auto',$isAuto)->sum('fee');
            $remark = "昵称:$user->nick_name($user->id)";
            foreach ($openids as $openid) {
                $app->template_message->send([
                    'touser' => $openid,
                    'template_id' => $templateId,
                    'url' => '',
                    'data' => [
                        'keyword1' => $memberOrder->fee.'元',
                        'keyword2' => $title.':'.$mapMemberType[$memberOrder->type]??'未知',//收款内容
                        'keyword3' => $memberOrder->created_at,//收款时间
                        'keyword4' => '过期时间:'.$user->expire_time,//交易状态
                        'keyword5' => $remark.'  今日总收益:'.$todayFee //交易说明
                    ]
                ]);
            }

        }
    }


    /**
     * Handle the MemberOrder "created" event.
     *
     * @param  \App\Models\User\MemberOrder  $memberOrder
     * @return void
     */
    public function created(MemberOrder $memberOrder)
    {
        //
    }

    /**
     * Handle the MemberOrder "updated" event.
     *
     * @param  \App\Models\User\MemberOrder  $memberOrder
     * @return void
     */
    public function updated(MemberOrder $memberOrder)
    {
        //
    }

    /**
     * Handle the MemberOrder "deleted" event.
     *
     * @param  \App\Models\User\MemberOrder  $memberOrder
     * @return void
     */
    public function deleted(MemberOrder $memberOrder)
    {
        //
    }

    /**
     * Handle the MemberOrder "restored" event.
     *
     * @param  \App\Models\User\MemberOrder  $memberOrder
     * @return void
     */
    public function restored(MemberOrder $memberOrder)
    {
        //
    }

    /**
     * Handle the MemberOrder "force deleted" event.
     *
     * @param  \App\Models\User\MemberOrder  $memberOrder
     * @return void
     */
    public function forceDeleted(MemberOrder $memberOrder)
    {
        //
    }
}
